﻿CREATE PROCEDURE [dbo].[usp_select_Configuration]
(
      @Name varchar(256) = NULL
	  ,@Debug   bit = 0
)
AS

/****************************************************************************
Name: usp_select_Configuration.sql
Description: Selects a configuration setting from the Configuration table.
*****************************************************************************/
SET NOCOUNT ON;

-- Bootstrap
DECLARE @DebugPrefix       varchar(10);
DECLARE @ErrorCode         int;

SET @ErrorCode          = 0;
SET @DebugPrefix        = '>>> DEBUG: ';


-- Test harness
/*
-- declare the stored procedure paramters

DECLARE @Name;

-- now set them
SELECT
	 @PARAM1  = 'LogSaves'
   ,@Debug                 = 1;

*/

-- Print parameters
IF (@Debug > 0)
BEGIN
    PRINT @DebugPrefix + '@Name = ' + CAST(@Name AS varchar);
END;

SELECT [Name], [Value] FROM dbo.Configuration WHERE [Name] = ISNULL(@Name, [Name])


-- CHECK FOR ERROR
SET @ErrorCode = @@Error;
IF (@ErrorCode <> 0) GOTO ERROR_HANDLER;
			
RETURN 0;

-- Handles errors.
ERROR_HANDLER:
IF (@ErrorCode <> 0)
BEGIN
    IF (@Debug > 0) 
		BEGIN
			PRINT @DebugPrefix + 'Error Code = ' + CAST(@ErrorCode AS varchar(10)) + ' occurred.';
			RETURN @ErrorCode
		END

    -- Don't return any evidence of what specific error occurred, i.e. 
    -- we want to ward off potential hackers.
    RETURN 1;
END;

SET NOCOUNT OFF;
